package com.ruoyi.exen.common.service.impl;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.exen.common.domain.Hrmdepartment;
import com.ruoyi.exen.common.domain.Hrmjobtitles;
import com.ruoyi.exen.common.domain.Hrmresource;
import com.ruoyi.exen.common.domain.Hrmsubcompany;
import com.ruoyi.exen.common.mapper.OrganizationalStructureMapper;
import com.ruoyi.exen.common.service.OrganizationalStructureService;
import com.ruoyi.system.domain.SysUserPost;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrganizationalStructureServiceImpl implements OrganizationalStructureService {

    @Autowired
    private OrganizationalStructureMapper mapper;

    @Override
    public List<Hrmresource> synchronousUsers() {

        //获取OA用户信息
        List<Hrmresource> OAUsers = mapper.getOAUsers();

        for (Hrmresource OAUser: OAUsers) {
            //OAUser.setUserId(Long.valueOf(OAUser.getId()));//用户id
            OAUser.setUserName(OAUser.getWorkcode());//工号
            OAUser.setNickName(OAUser.getLastname());//名字
            OAUser.setDeptId(OAUser.getDepartmentid());//部门id
            OAUser.setPhonenumber(OAUser.getMobile());//手机号
            OAUser.setUpdateTime(new Date());
            OAUser.setStatus("0");
            OAUser.setDelFlag("0");
            OAUser.setEmail(OAUser.getEmail());//邮箱
            //重置密码
            OAUser.setPassword(SecurityUtils.encryptPassword(OAUser.getWorkcode()));

            //判断此工号是否存在
            Integer userId=mapper.checkUserByJobNumber(OAUser.getWorkcode());

            if (ObjectUtils.isEmpty(userId)){
                //不存在，新增

                mapper.insertOAUser(OAUser);
            }else {
                //存在
                mapper.updateOAUser(OAUser);
            }
        }
        return OAUsers;
    }


    /**
     * 同步OA分部层
     * @return 分部信息
     */
    @Override
    public List<Hrmsubcompany> synchronousHrmsubcompany() {
        List<Hrmsubcompany> hrmsubcompany = mapper.getHrmsubcompany();
        for (Hrmsubcompany h : hrmsubcompany) {
            if(StringUtils.isNotEmpty(h.getSubcompanyname())){
                h.setModified(new Date());
                Hrmsubcompany checkHrmsubcompany = mapper.checkHrmsubcompany(String.valueOf(h.getId()));
                if (org.springframework.util.ObjectUtils.isEmpty(checkHrmsubcompany)){
                    mapper.insertHrmsubcompany(h);
                }else {
                    mapper.updateHrmsubcompany(h);
                }
            }
        }
        return hrmsubcompany;
    }

    /**
     * 同步OA部门层
     * @return 分部信息
     */
    @Override
    public List<Hrmdepartment> synchronousDept() {
        List<Hrmdepartment> OADept = mapper.getSysDepts();
        for (Hrmdepartment d : OADept) {
            d.setDeptId(Long.valueOf(d.getId()));
            d.setParentId(d.getSupdepid());
            d.setDeptName(d.getDepartmentname());
            d.setStatus("0");
            d.setDelFlag("0");
            if (StringUtils.isNotEmpty(d.getDepartmentname())){
                Integer deptId = mapper.checkDept(Math.toIntExact(d.getId()));
                if (org.springframework.util.ObjectUtils.isEmpty(deptId)){
                    mapper.insertDept(d);
                }else {
                    mapper.updateDept(d);
                }
            }
        }
        return OADept;
    }

    /**
     * 同步岗位
     * @return
     */
    @Override
    public List<Hrmjobtitles> synchronousPost() {
        List<Hrmjobtitles> sysPosts = mapper.getSysPosts();
        for (Hrmjobtitles s : sysPosts) {
            s.setPostId(Long.valueOf(s.getId()));
            s.setPostName(s.getJobtitlemark());
            s.setStatus("0");
            s.setPostSort(1);

            s.setPostCode(s.getJobtitlemark());
            if (!org.springframework.util.ObjectUtils.isEmpty(s.getId())) {
                Integer postId = mapper.checkPost(Math.toIntExact(s.getId()));
                if (org.springframework.util.ObjectUtils.isEmpty(postId)) {
                    mapper.insertPost(s);
                } else {
                    mapper.updatePost(s);
                }
            }
        }
        return sysPosts;
    }


    /**
     * 同步人员对应岗位
     */
    @Override
    public void synchronousUerAndPost() {

        List<SysUser> usersByExen = mapper.getUsersByExen();
        for (SysUser user: usersByExen) {
            Long userId = user.getUserId();
            Long jobtitle = user.getJobtitle();
            SysUserPost sysUserPost = mapper.checkUserAndPost(userId, jobtitle);
            if (org.springframework.util.ObjectUtils.isEmpty(sysUserPost)){
                if (userId != null && jobtitle !=null){
                    mapper.insertUserAndPost(userId,jobtitle);
                }

            }
        }


    }
}
